import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

/* Router Modules */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRouterMap = [
  // 登录
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' }
  },
  // 一级页面
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/home/index'),
    meta: { title: '首页' }
  },
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('@/views/mine/index'),
    meta: { title: '我的', roles: true }
  },
  // 搜索页面
  {
    path: '/search/house',
    name: 'SearchHouse',
    component: () => import('@/views/search/house'),
    meta: { title: '搜索-房源' }
  },
  // 房屋列表
  {
    path: '/house/list',
    name: 'HouseList',
    component: () => import('@/views/house/list'),
    meta: { title: '房屋列表' }
  },
  // 房屋详情
  {
    path: '/house/detail',
    name: 'HouseDetail',
    component: () => import('@/views/house/detail'),
    meta: { title: '房屋详情' }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404'),
    meta: { title: '404' }
  },
  // 404 页面必须放在最后面
  { path: '*', redirect: '/404', hidden: true }
]

export const asyncRouterMap = []

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    routes: constantRouterMap,
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        return {
          x: 0,
          y: 0
        }
      }
    }
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
